 
import { ABaseDecision } from "GameLoop/DecisionMaking/BaseDecision";
import { priority } from "utils/constants/definetypes";
import { TaskHelper } from "utils/task/TaskHelper";
 

// 还要添加一个定时器 事件触发器
/**
 * // 得整理一下 minerial的执行流程
 * 1.本房间的元素矿
 * 2.9矿房的元素矿
 * minerial 采集任务
 */
export class HarvestMinerialDecision<  T extends "creep"="creep"> extends ABaseDecision<T>
{
    // 找不到任务。就新增
    public isExpired(event: NormalEvent, region: string): boolean 
    {
        const room = Game.rooms[event.region] as Required<Room>;
        if(room.canExecute(150) && room.controller.level>=6 && room.getEntity("CoreEntity").freeSpace()>50000  )
            return true;
        return false
    }
    public makeTask(event: NormalEvent, region: string): IDecisionCode<T>|false
    {
        
        const room = Game.rooms[event.region] as Required<Room>;
        const  Mine = room.getEntitys("MinerialEntity").concat(room.getEntitys("OutMinerialEntity"));
        
        // 俩矿工俩采集工
        const tasks=[];

        for(const e of Mine)
        {
            if(e.getNodeMemory().state)
            {
                room.deleteTaskById(e.GUID(),"creep")
                continue;
            }
            if(!e.room?.getStructures(STRUCTURE_EXTRACTOR).length)
            {
                continue;
            }

            if(!e.getMinerial()?.mineralAmount)
            {
                continue;
            }
            
            const curTask = room.findTaskById<ITask<ICreepTaskData<"harvestM">>>( e.GUID(),"creep");
            if(curTask)
            {
                
                curTask.data.arg = {
                    entityId:e.GUID(),
                }
                curTask.data.required = [
                    [ ["minex"],1,1],
                    [["porter"],1,1]
                ]
            }
            else
            {
                const task:ITaskDeclare["creep"]={
                    id:  e.GUID(),
                    priority:priority.C,
                    ttl:-1,
                    type:"creep",
                    data:{
                        type:"harvestM",
                        arg:{
                            entityId:e.GUID(),
                        },
                        room:e.roomName,
                        creeps:[],
                        body:{},        // 当前统计的body数量
                        income: 0, // 1500T内的收入
                        cost:TaskHelper.getTaskPathCost(event.region,event.room),
                        required:[
                            [ ["minex"],1,1],
                            [["porter"],1,1]
                        ]
                    },
                    shard:1,
                }
                tasks.push(task);
            }
            
        }
        if(tasks.length>0)
        {
            const code:IDecisionCode<T>={
                region:event.region,
                task:tasks,
                isunique:false,
            }
            return code
        }
        return false
    }
    
}